﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class DepVA_Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                //机场服务可能外包给其他公司（如其他航空公司，或机场当局）！

                if (Request.QueryString["flt_no"] != null)
                {
                    lstAirlines.SelectedValue = Request.QueryString["airline"];
                    txtFltNo.Text = Request.QueryString["flt_no"].ToString();
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
                    //btnRetreive_Click(sender, e);
                }
                else
                {
                    if (Session["FLT_NO"] != null)
                        txtFltNo.Text = Session["FLT_NO"].ToString();
                    if (Session["FLT_DATE"] != null)
                        txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
                }
                lblDcsAirport.Text = Session["DCS_AIRPORT"].ToString();
            }
            lblError.Text = "";
            //btnSetStatus.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            //btnTransfer.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
            ctlSeatMatching.compare(lstAirlines.SelectedValue, txtFltNo.Text, Convert.ToDateTime(txtFltDate.Text));
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DB2") < 0)
            {
                lblErrChange.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblErrChange.Text = "";
            try
            {
                cmd.CommandText = "bll_flt_batch_set_tkt_seat_type_by_emd";
                SqlParameter para = cmd.Parameters.AddWithValue("@op_airline", lstAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@op_flt_no", txtFltNo.Text);
                para = cmd.Parameters.AddWithValue("@flt_date", txtFltDate.Text);
                para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());
                //para = cmd.Parameters.AddWithValue("@is_group", chkIsGroup.Checked);
                //para = cmd.Parameters.AddWithValue("@dcs_status", lstStatus.SelectedValue);
                para = cmd.Parameters.AddWithValue("@new_seat_type", txtNewValue.Text);
                para = cmd.Parameters.AddWithValue("@where_clause", txtWhere.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrChange.Text = "Done.";
                    GridView1.DataBind();
                    ctlSeatMatching.compare(lstAirlines.SelectedValue, txtFltNo.Text, Convert.ToDateTime(txtFltDate.Text));
                    return;
                }
                else if (ret_code == 1)
                    lblErrChange.Text = "Illegal verbs";
                else if (ret_code == 2)
                    lblErrChange.Text = "Unauthorized airline!";
                //else if (ret_code == 3)
                //    lblErrChange.Text = "!";
                else if (ret_code == 4)
                    lblErrChange.Text = "Updating failed!";
                else
                    lblErrChange.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrChange.Text = ex.Message;
                else
                    lblErrChange.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}